#include <stdio.h>
#include <stdlib.h>
struct node
{
  
int data;
   
struct node *next;
 
};

struct node *first;

struct node *
add_node (int num, struct node *p)	//comment                   /*Inserts an element in the end.*/ 
{
  
struct node *g, *k, *f;
  
f = p;
  
k = p;
  
g = (struct node *) malloc (sizeof (struct node));
  
g->data = num;
  
if (f == NULL)
    {
      
f = g;
      
f->next = NULL;
    
}
  

  else
    {
      
while (k->next != NULL)
	
k = k->next;
      
k->next = g;
      
k = g;
      
k->next = NULL;
    
}
  

return f;

}



struct node *
del_node (int x, struct node *p) 
  /*Deletes the first element equal to x in the list. */ 
{
  

struct node *prev, *f, *k;
  
f = p;
  
k = p;
  
int u = 0;
  
prev = NULL;
  
while (f != NULL)
    {
      
if (f->data == x)
	{
	  
break;
	
}
      

      else
	{
	  
prev = f;
	  
f = f->next;
	  
u = u + 1;
	
}
    
}
  
if (u == 0)
    
k = k->next;
  

  else if (u > 0 && f != NULL)
    
prev->next = f->next;
  

  else
    
printf ("The node doesn't exist.");
  

return k;

}



int
count (int x, struct node *p) 
  /*Determines the no. of times x appeared in the list. */ 
{
  
int count = 0;
  
struct node *f;
  
f = p;
  
while (f != NULL)
    {
      
if (f->data == x)
	{
	  
count = count + 1;
	  
f = f->next;
	
}
      

      else
	{
	  
f = f->next;
	
}
    
}
  
return count;

}



int
max (struct node *p) 
{
  
struct node *f;
  
f = p;
  
int a;
  
a = f->data;
  
while (f != NULL)
    
    {
      
if (a < f->data)
	
	{
	  
a = f->data;
	  
f = f->next;
	
}
      
      else
	
f = f->next;
    
}
  
return a;

}



int
min (struct node *p) 
{
  
struct node *f;
  
f = p;
  
int a;
  
a = f->data;
  
while (f != NULL)
    
    {
      
if (a > f->data)
	
	{
	  
a = f->data;
	  
f = f->next;
	
}
      
      else
	
f = f->next;
    
}
  
return a;

}




struct node *
reverse (struct node *p) 
{
  
struct node *f, *k, *g, *m;
  
f = p;
  
g = (struct node *) malloc (sizeof (struct node));
  
k = NULL;
  
while (f != NULL)
    
    {
      
if (k == NULL)
	
	{
	  
k = g;
	  
k->data = f->data;
	  
k->next = NULL;
	
}
      
      else
	
	{
	  
m->data = f->data;
	  
m->next = k;
	  
k = m;
	  
m = g;
	
}
      
f = f->next;
    
}
  

return k;

}




display (struct node * p) 
{
  
while (p != NULL)
    {
      
printf ("%d ", p->data);
      
p = p->next;
    
}

}

printf ("This program implements python lists in C.Function list:\n 1.Delete  2.Count 3.Maximum 4.Minimum 5.Reverse 0.Exit ")

void
main () 
{
  
int n, b, i, h, r, c, cnt, mx, t, mn;
  
struct node a;
  
struct node *p, *rev;
  
p = malloc (n * sizeof (*p));
  
p = NULL;
  
printf ("Enter the length of the list:");
  
scanf ("%d", &n);
  
for (i = 0; i < n; i++)
    {
      
printf ("Enter a no.:");
      
scanf ("%d", &h);
      
p = add_node (h, p);
    
}
  
printf ("The list is: \n");
  
display (p);
  
while (1)
    
    {
      
printf ("Enter the function you would like to use: \n");
      
scanf ("%d", &b);
      
switch (b)
	
	{
	
case 1:
	  {
	    
printf ("Enter the no. to be deleted:");
	    
scanf ("%d", &t);
	    
p = del_node (t, p);
	    
display (p);
	    
break;
	  
}
	
case 2:
	  {
	    
printf ("Enter the no. whose frequency you want to find:");
	    
scanf ("%d", &c);
	    
cnt = count (c, p);
	    
printf ("Count=%d \n", cnt);
	    
break;
	  
}
	
case 3:
	  {
	    
mx = max (p);
	    
printf ("Maximum no.:%d ", mx);
	    
break;
	  
}
	
case 4:
	  {
	    
mn = min (p);
	    
printf ("Min=%d \n", mn);
	    
break;
	  
}
	
case 5:
	  {
	    
printf ("Reverse list:");
	    
display (rev);
	    
break;
	  
}
	
case 0:
	  {
	    
exit (0);
	    
break;
	  
}
	
}
    
}

}


